<?php

$dirname = $_POST['mapname'];

$dir = opendir("../images/$dirname/maps/coords/");


$files = array(); //holds all images name

$filecount = 0; //used for $files array
$changecolor; //will store color-change image for backmap

while(false != ($file = readdir($dir))){
	if($file != "." && $file != ".."){ //fetch all files excluding parentfolders

		//make images for frontmap

		$files[$filecount] = $file; //place filename in array
		$coordfile = file_get_contents("../images/$dirname/maps/coords/" . $files[$filecount], 'r'); //read the json-file

		$imgcoords = json_decode($coordfile); //fetch the coords to draw polygon

		$arraynumb = count($imgcoords -> x);
		$r = $imgcoords -> rcol;
		$g = $imgcoords -> gcol;	//fetch the colors from json-file
		$b = $imgcoords -> bcol;

		$image = imagecreatetruecolor(960, 804); //create canvas of same size as map
		imagesavealpha($image, true);

		$trans_color = imagecolorallocatealpha($image, 0, 0, 0, 127); //make background of canvas transparent
		imagefill($image, 0, 0, $trans_color);

		$color = imagecolorallocatealpha($image, $r, $g, $b, 80); // color of map

		$arraycoords = array();
		$j = 0;

		for($i=0;$i<$arraynumb; $i++){ //structure the json-coords into an array suitable for GD
			$arraycoords[$j] = $imgcoords -> x[$i];
			$j++;
			$arraycoords[$j] = $imgcoords -> y[$i];
			$j++;
		}
		imagefilledpolygon($image, $arraycoords, $arraynumb, $color); //draw figure

		$files[$filecount] = strstr($files[$filecount], '.', TRUE); //remove .json suffix from filename

		$filename = "../images/$dirname/maps/front" . $files[$filecount] . '.png'; //create filename-string and dir placement

		imagepng($image,$filename); //save the file

		imagedestroy($image); //close canvas

		//make images for backmap

		$image = imagecreatetruecolor(960, 804); //create new canvas
		imagesavealpha($image, true);

		$trans_color = imagecolorallocatealpha($image, 0, 0, 0, 127); //make canvas transparent
		imagefill($image, 0, 0, $trans_color);


		$arraycoords = array();
		$j = 0;

		$changecolor = $imgcoords -> backcolor;

		for($i=0;$i<$arraynumb; $i++){
			$arraycoords[$j] = $imgcoords -> x[$i];
			$j++;
			$arraycoords[$j] = $imgcoords -> y[$i];
			$j++;
			$color = imagecolorallocatealpha($image, $changecolor, 0, 0, 100);
		}
		imagefilledpolygon($image, $arraycoords, $arraynumb, $color);

		//$backcolorarray[] = "'" . $files[$filecount] . "' : " . $changecolor;

		$filename = "../images/$dirname/maps/back" . $files[$filecount] . '.png';

		imagepng($image,$filename);

		imagedestroy($image);
	}
}

closedir($dir);

//draw backmap

$dir = opendir("../images/$dirname/maps/");
$backcanvas = imagecreatetruecolor(960,804);
imagesavealpha($backcanvas, true);
$trans_color = imagecolorallocatealpha($backcanvas, 0, 0, 0, 127); //make background of canvas transparent
imagefill($backcanvas, 0, 0, $trans_color);

while(false != ($file = readdir($dir))){ //loop through map and print all images found with .png suffix
	if(strpos($file, '.png') && strstr($file, 'back')){
		$backsrc = imagecreatefrompng("../images/$dirname/maps/$file");
		imagecopy($backcanvas, $backsrc, 0, 0, 0, 0, 960, 804);
	}
}

imagepng($backcanvas,"../images/$dirname/mapback.png");

imagedestroy($backcanvas);
imagedestroy($backsrc);
closedir($dir);

$dir = opendir("../images/$dirname/maps/");
$frontcanvas = imagecreatetruecolor(960,804);
imagesavealpha($frontcanvas, true);
$trans_color = imagecolorallocatealpha($frontcanvas, 0, 0, 0, 127); //make background of canvas transparent
imagefill($frontcanvas, 0, 0, $trans_color);

$frontsrc = imagecreatefromjpeg('../images/map.jpg');
imagecopy($frontcanvas, $frontsrc, 0, 0, 0, 0, 960, 804);

while(false != ($file = readdir($dir))){ //loop through map and print all images found with .png suffix
	if(strpos($file, '.png') && strstr($file, 'front')){
		$frontsrc = imagecreatefrompng("../images/$dirname/maps/$file");
		imagecopy($frontcanvas, $frontsrc, 0, 0, 0, 0, 960, 804);
	}
}

imagejpeg($frontcanvas,"../images/$dirname/mapfront.jpg");

imagedestroy($frontcanvas);
imagedestroy($frontsrc);

closedir($dir);

header("Location: ../index.php?p=addunits&mapid=$dirname");

?>